import request from '@/utils/request.js'
import { ElMessage } from 'element-plus'

//获取饼图数据
export function getChartData(category) {
    const params = { category }
    return request({
        url: '/admin/index/pieChart',
        method: "GET",
        params,
    })
}

//获取预储总人数
export function getPreCount() {
    return request({
        url: '/admin/preReserve/preReserveCount',
        method: 'GET'
    })
}

//分页查询预储成员信息
export function getPage(params) {
    return request({
        url: "/admin/preReserve/members",
        method: "GET",
        params,
    })
}

//导出多个预储成员列表数据
export function getMemberList(data) {
    return request({
        url: "/admin/preReserve/membersExcelList",
        method: "POST",
        responseType: 'blob', // 重要：确保响应类型为 blob
        data
    }).then(response => {
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'Members_list.xlsx'); // 设置下载文件名
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        ElMessage.success("导出成功！");
    }).catch(error => {
        ElMessage.error("导出失败！");
        console.error('导出失败', error);
    });
}

//导出excel
export function getExcle(data) {
    return request({
        url: "/admin/preReserve/membersExcel",
        method: "POST",
        responseType: 'blob', // 确保响应类型为 blob
        data
    }).then(response => {
        //从响应头获取文件名
        const contentDisposition = response.headers['content-disposition'];
        let fileName = 'preReserve_members_PDF.zip'; // 默认文件名
        if (contentDisposition && contentDisposition.includes('filename=')) {
            fileName = contentDisposition.split('filename=')[1];
        }
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', fileName); // 使用后端返回的文件名
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        ElMessage.success("导出成功！");
    }).catch(error => {
        ElMessage.error("导出失败！");
        console.error('导出失败', error);
    });
}

//导出pdf
export function getPdf(data) {
    return request({
        url: "/admin/preReserve/membersPDF",
        method: "POST",
        responseType: 'blob', // 确保响应类型为 blob
        data
    }).then(response => {
        const contentDisposition = response.headers['content-disposition'];
        let fileName = ''; // 默认文件名
        if (contentDisposition && contentDisposition.includes('filename=')) {
            fileName = contentDisposition.split('filename=')[1];
        }
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', fileName); // 使用后端返回的文件名
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        ElMessage.success("导出成功！");
    }).catch(error => {
        ElMessage.error("导出失败！");
        console.error('导出失败', error);
    });
}

